<?php
/****
 * @example
 * $url="http://www.google.com/intl/en/press/index.html";
 * $sock=new zSocket($url);
 * $source=$sock->GetText();
 **/
class Socket
{
	var $url;
	var $socket;
	var $timeout=30;
	function Socket($url,$timeout=30)
	{
		$this->url=parse_url(&$url);
		$this->timeout=&$timeout;
		if (!isset($this->url['scheme']) OR !trim($this->url['scheme'])) $this->url['scheme']='http';
		if (!isset($this->url['host']) OR !trim($this->url['host'])) $this->url['host'] ='localhost';
		if (!isset($this->url['port']) OR !trim($this->url['port'])) $this->url['port']=80;
		if (!isset($this->url['user']) OR !trim($this->url['user'])) $this->url['user']='';
		if (!isset($this->url['pass']) OR !trim($this->url['pass'])) $this->url['pass']='';
		if (!isset($this->url['path']) OR !trim($this->url['path'])) $this->url['path']='/index.rss';
		if (!isset($this->url['query']) OR !trim($this->url['query'])) $this->url['query']='';
		if (!isset($this->url['fragment']) OR !trim($this->url['fragment'])) $this->url['fragment']='';
	}
 
	/**
 	 * Открываем socket и пишем в него заголовки
	 *
	 * @param array $request_headers
	 * @return boolean
	 */
 	function Open($request_headers=NULL)
 	{
 		$request='';
 		$request_headers['GET']=$this->url['path'].'?'.$this->url['query'].'#'.$this->url['fragment']." ".strtoupper($this->url['scheme'])."/1.1";
 		$request_headers['Host:']=$this->url['host'];
 		$request_headers['Content-type:']='application/x-www-form-urlencoded';
 		$request_headers['User-Agent:']='MSIE';
 		$request_headers['Connection:']='close';
		// создаём строку-запрос
		foreach ((array)$request_headers as $k=>$v)
		{
			$request.=$k." ".$v."\n";
		}
		// var_export($request);
 
		// открываем сокет
		if ($this->socket = fsockopen($this->url['host'],$this->url['port']))
		{
			// пишем в него
			$this->Write($request."\n");
			return true;
		}
		else 
		{
			trigger_error("Can't create socket",E_USER_ERROR);
			return false;
		}
	}
	
	/**
	 * читаем из сокета
	 *
	 * @return string
	 */
	function Read($buffer_length=4096)
	{
		if (!feof($this->socket))
		{
			$str=fgets($this->socket,$buffer_length);
			return $str;
		}
		return '';
	}
	/**
	 * Пишем в сокет
	 *
	 * @param string $message
	 */
	function Write($message)
	{
		fputs($this->socket, $message."\n");
	}
 
	/**
	 * Закрываем сокет
	 *
	 */
	function Close()
	{
		if ($this->socket)
		{
			fclose($this->socket);
		}
		else
		{
			trigger_error("Can't find socket to close",E_USER_NOTICE);
		}
	}
	
	/**
	 * Получает весь ответ от сервера
	 *
	 */
	function GetResponse()
	{
		$result='';
		while ($str=$this->Read())
		{
			$result.=$str;
		}
		return $result;
	}

	
	function GetText()
	{
		if (!$this->socket)
		{
			if (!$this->Open())
			{
				return false;
			}
		}
		$result='';
		while ($str=$this->Read())
		{
			$result.=$str;
		}
		$a=strpos($result,"\r\n\r\n");
		$header=substr($result,0,$a);
		if (!preg_match("/HTTP\/1\.\d 2\d\d/",$header))
		{
			return false;
		}
		$result=substr($result,$a+4);
		return $result;
	}

	/*function GetText()
	{
		if(!$this->socket)
		{
			if (!$this->Open())
			{
				return false;
			}
		}
		$source_text=$this->Read();
		$this->Close();
		// вырезаем HTTP header
		$res=preg_split("/((\r\n\r\n))/",$source_text,2);
		if (is_array($res) && sizeof($res)==2)
		{
			$this->response=$res[0];
			$page_content=$res[1];
			if (!preg_match("/^HTTP\/\d\.?\d? 2\d{2} /",$this->response))
			{
				trigger_error("Page Not Found",E_USER_WARNING);
				return false;
			}
		}
		else
		{
			trigger_error("I dont know how parse this content",E_USER_ERROR);
		}
		$page_content=trim($page_content);
		return $page_content;
	}*/
}
?>